package com.puzzlebazar.client.main.control;

import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.puzzlebazar.client.admin.SiteInfo;
import com.puzzlebazar.client.admin.SiteModel;
import com.puzzlebazar.client.main.view.MainView;
import com.puzzlebazar.client.user.UserModel;
import com.puzzlebazar.client.util.Observable;
import com.puzzlebazar.client.util.Observer;

public class WelcomeState extends MainState implements Observer {
	
	public static final String STATE_TOKEN = "WelcomeState";
	
	private final SiteModel settingsModel;
	private final VerticalPanel mainPanel = new VerticalPanel();
	private final HTML description = new HTML();
	private final HTML welcomeMessage = new HTML();

	private boolean isInside = false;	
	
	public WelcomeState(SiteModel settingsModel, UserModel model,
			MainView view, MainController mainController) {
		super( model, view, mainController );

		this.settingsModel = settingsModel;
		
		final HorizontalPanel descriptionBar = new HorizontalPanel();
		descriptionBar.setWidth( "100%" );
		descriptionBar.setStylePrimaryName( "descriptionBar" );
		
		descriptionBar.add( description );
		
//		welcomeMessage.setWidth( "100%" );		
		welcomeMessage.setStylePrimaryName( "mainContent" );

		mainPanel.setWidth( "100%" );		
		mainPanel.add( descriptionBar );
		mainPanel.add( welcomeMessage );

		settingsModel.addObserver( this );
	}

	@Override
	public String getStateToken() {
		return STATE_TOKEN;
	}
	

	@Override
	public void enter() {
		isInside = true;
		
		setText();
		
		view.setMainWidget( mainPanel );
	}

	@Override
	public void exit() {
		isInside = false;
	}

	@Override
	public void update(Observable observable, Object arg) {
		if( isInside )
			setText();		
	}

	private void setText() {
		SiteInfo siteInfo = settingsModel.getSiteInfo();
		view.setMainPanelTitle( "Welcome to " + siteInfo.getSiteName() );
		description.setHTML(siteInfo.getSiteDescription());
		welcomeMessage.setHTML(siteInfo.getWelcomeMessage());
	}


	@Override
	public void release() {
		settingsModel.deleteObserver(this);
	}
}
